import Taro from "@tarojs/taro";
import { getToken } from "./auth";
const handleError = (code) => {
  switch (code) {
    case 400:
      console.error('请求错误');
      break;
    case 401:
      console.error('未授权，请重新登录');
      break;
    case 403:
      console.error('拒绝访问');
      break;
    case 404:
      console.error('请求地址出错');
      break;
    case 500:
      console.error('服务器内部错误');
      break;
    default:
      console.error(`其他错误，状态码：${code}`);
  }
};

const doFetch = (options) => {
  const { url, method = 'POST', params } = options;

  //获取 token
  var token = getToken();
  console.log(token, APPURL)
  const headers = {
    Authorization: token,
  };

  return Taro.request({
    url: url.includes('/map') ? `https://www.youtianchen.cn${url}` : `${APPURL}${url}`,
    method,
    data: { ...params },
    header: {
      'Content-Type': 'application/json',
      ...headers,
    },
  });
};

export default doFetch;
